package com.link510.aitools.services;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.blankj.utilcode.util.Utils;
import com.link510.aitools.core.constants.FilterConstants;
import com.link510.aitools.core.domain.message.TcpMessageInfo;
import com.orhanobut.logger.Logger;


/**
 * 广播
 */
public class FilterUtils {


    private static final String TAG = "FilterUtils";


    /**
     * 发送广播
     *
     * @param action
     */
    public static void send(String action) {

        send(Utils.getApp(), action, new TcpMessageInfo());

    }

    /**
     * 发送广播
     *
     * @param action
     * @param message
     */
    public static void send(String action, String message) {
        send(Utils.getApp(), action, message);
    }

    /**
     * 发送广播
     *
     * @param action
     */
    public static void send(String action, TcpMessageInfo tcpMessageInfo) {


        send(Utils.getApp(), action, tcpMessageInfo);
    }

    /**
     * 发送广播
     *
     * @param context 上下文
     * @param action  广告action
     */
    public static void send(Context context, String action) {

        send(context, action, new TcpMessageInfo());
    }

    /**
     * 发送广播
     *
     * @param context
     * @param action
     * @param message
     */
    public static void send(Context context, String action, String message) {

        TcpMessageInfo tcpMessageInfo = new TcpMessageInfo();

        tcpMessageInfo.setMessage(message);
        tcpMessageInfo.setState(0);

        send(context, action, tcpMessageInfo);
    }

    /**
     * 发送广播
     *
     * @param context 上下文
     * @param action  广告action
     */
    public static void send(Context context, String action, TcpMessageInfo tcpMessageInfo) {

        try {

            Intent intent = new Intent(action);

            intent.putExtra(FilterConstants.FILTER_PARAMETER_KEY, tcpMessageInfo);


            context.sendBroadcast(intent);
            Logger.e("发送广播成功");
            Logger.e("action:" + action);

        } catch (Exception ex) {

            Logger.e("发送广播失败", ex);
        }

    }


    /**
     * 注册消息
     *
     * @param broadcastReceiver
     * @param actionArgs
     */
    public static void registerReceiver(BroadcastReceiver broadcastReceiver, String... actionArgs) {


        registerReceiver(Utils.getApp(), broadcastReceiver, actionArgs);
    }

    /**
     * 注册消息
     *
     * @param broadcastReceiver
     * @param actionArgs
     */
    public static void registerReceiver(Context context, BroadcastReceiver broadcastReceiver, String... actionArgs) {


        if (actionArgs.length <= 0) {
            return;
        }

        IntentFilter intentFilter = new IntentFilter();

        for (String s : actionArgs) {

            intentFilter.addAction(s);
        }

        registerReceiver(context, intentFilter, broadcastReceiver);

    }

    /**
     * 注册消息
     *
     * @param intentFilter
     * @param broadcastReceiver
     */
    public static void registerReceiver(IntentFilter intentFilter, BroadcastReceiver broadcastReceiver) {


        registerReceiver(Utils.getApp(), intentFilter, broadcastReceiver);

    }

    /**
     * 注册消息
     *
     * @param context
     * @param intentFilter
     * @param broadcastReceiver
     */
    public static void registerReceiver(Context context, IntentFilter intentFilter, BroadcastReceiver broadcastReceiver) {

        try {

            context.registerReceiver(broadcastReceiver, intentFilter);
        } catch (Exception ex) {
            Logger.e("注册广播失败: ", ex);
        }
    }

    /**
     * 注消注册消息
     *
     * @param broadcastReceiver
     */
    public static void unRegisterReceiver(BroadcastReceiver broadcastReceiver) {
        unRegisterReceiver(Utils.getApp(), broadcastReceiver);
    }

    /**
     * 注消注册消息
     *
     * @param context
     * @param broadcastReceiver
     */
    public static void unRegisterReceiver(Context context, BroadcastReceiver broadcastReceiver) {

        try {
            context.unregisterReceiver(broadcastReceiver);
        } catch (Exception ex) {
            Logger.e("注消广播失败: ", ex);
        }
    }


}
